package homelesspartners.client.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

public class Shelter implements Serializable {

	private static final String SPACE = " ";
	private static final long serialVersionUID = 1L;
	private int id = -1;
	private String name = null;
	private int cityId = -1;
	private boolean populated = false;
	private String address = null;
	private String phone = null;
	private String website = null;
	private String whatTheyDo = null;
	private String whatTheyNeed = null;
	private String additionalDetails = null;
	private String idFormat = null;

	/**
	 * @gwt.typeArgs <homelesspartners.client.model.Story>
	 */
	private ArrayList stories = new ArrayList();

	/**
	 * @gwt.typeArgs <homelesspartners.client.model.Story>
	 */
	private ArrayList pagedStories = new ArrayList();
	private int storyOffset = 0;
	private boolean loading = false;

	public static final int PAGE_SIZE = 20;

	public void setId(int anId) {
		this.id = anId;
	}

	public void setName(String aName) {
		this.name = aName;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int aCityId) {
		cityId = aCityId;
	}

	public ArrayList getStories() {
		return stories;
	}

	public void addStory(Story aStory) {
		if (!stories.contains(aStory)) {
			stories.add(aStory);
			aStory.setShelter(this);
		}
	}

	public boolean equals(Object obj) {
		Shelter compared = (Shelter) obj;
		if (this.getId() == compared.getId()) {
			return true;
		}
		return false;
	}

	public void deleteStory(Story story) {
		stories.remove(story);
	}

	public boolean isPopulated() {
		return populated;
	}

	public void setPopulated(boolean value) {
		this.populated = value;
	}

	public String getAdditionalDetails() {
		return additionalDetails;
	}

	public void setAdditionalDetails(String additionalDetails) {
		this.additionalDetails = additionalDetails;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getWhatTheyDo() {
		return whatTheyDo;
	}

	public void setWhatTheyDo(String text) {
		this.whatTheyDo = text;
	}

	public String getWhatTheyNeed() {
		return whatTheyNeed;
	}

	public void setWhatTheyNeed(String text) {
		this.whatTheyNeed = text;
	}

	public String getAddressText() {
		return getAddress() + ", Phone: " + getPhone();
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(name);
		buffer.append(SPACE);
		buffer.append(address);
		buffer.append(SPACE);
		buffer.append(phone);
		buffer.append(SPACE);
		buffer.append(idFormat);
		buffer.append(SPACE);
		buffer.append(whatTheyDo);
		buffer.append(SPACE);
		buffer.append(whatTheyNeed);
		buffer.append(SPACE);
		buffer.append(additionalDetails);
		return buffer.toString();
	}

	public String validate() {
		if (name == null || name.length() == 0) {
			return "Please enter a name.";
		}
		if (address == null || address.length() == 0) {
			return "Please enter an address.";
		}
		if (phone == null || phone.length() == 0) {
			return "Please enter a phone number.";
		}
		if (idFormat == null || idFormat.length() == 0) {
			return "Please enter a format for the assigned ids.";
		}
		if (whatTheyDo == null || whatTheyDo.length() == 0) {
			return "Please enter a brief description of what this shelter does (eg. clients, services, etc)";
		}
		if(additionalDetails != null && additionalDetails.length() > 2048) {
			return "Please shorten the additional details.  It must be less than 2048 characters.";
		}
		return null;
	}

	public boolean hasMore() {
		int remainder = pagedStories.size() - storyOffset;
		if (remainder >= PAGE_SIZE || remainder == 0) {
			return true;
		}
		return false;
	}

	public boolean hasPrevious() {
		if (storyOffset == 0) {
			return false;
		}
		return true;
	}

	public ArrayList getPagedStories() {
		return pagedStories;
	}

	public void setPageOfStories(ArrayList pageOfStories) {
		for (Iterator storyIter = pageOfStories.iterator(); storyIter.hasNext();) {
			Story story = (Story) storyIter.next();
			if (!pagedStories.contains(story)) {
				story.setShelter(this);
				pagedStories.add(story);
			}
		}
	}

	public int getStoryOffset() {
		return storyOffset;
	}

	public void goNext() {
		storyOffset = storyOffset + PAGE_SIZE;
	}

	public void goPrevious() {
		storyOffset = storyOffset - PAGE_SIZE;
	}

	public boolean isPagePopulated() {
		if (pagedStories.size() > storyOffset) {
			return true;
		}
		return false;
	}

	public void setLoading(boolean b) {
		loading = b;
	}

	public boolean isLoading() {
		return loading;
	}

	
	public String getIdFormat() {
		return idFormat;
	}

	
	public void setIdFormat(String idFormat) {
		this.idFormat = idFormat;
	}

	
	public String getWebsite() {
		return website;
	}

	
	public void setWebsite(String website) {
		this.website = website;
	}

	public void removeStory(Story story) {
		stories.remove(story);
	}
}
